package day7;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;

class Person{
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Person(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(name, person.name) &&
                Objects.equals(age, person.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

public class Demo1 {

    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add(new Person("刘二", 19));
        c.add(new Person("张三", 20));
        c.add(new Person("李四", 21));
        c.add(new Person("王五", 22));
        c.add(new Person("梁六", 23));

        Iterator<Person> p = c.iterator();
        while (p.hasNext()){
            Person per = (Person) p.next();
            System.out.println("姓名: " + per.getName() + "\t年龄: " + per.getAge());
        }
    }

}
